<?php
/*    This file is part of My Web Backup.
 *
 *    My Web Backup is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    My Web Backup is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with My Web Backup.  If not, see <http://www.gnu.org/licenses/>.
 */

class IndexController extends Zend_Controller_Action
{
	protected $validCommands = array (
			'backup',
			'search',
			'file',
			'restore'
		);
	
	public function preDispatch()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
	}
	
	public function init()
	{
		
	}

	public function indexAction()
	{
		$mwb = new Default_Model_MyWebBackup();
		
		$request = $this->getRequest();
		
		if (!$this->authenticate($mwb, $request)) {
			// error out here.
		}
		
		$type = $request->getParam('type');
		
		if (in_array ($type, $validCommands)) {
			$method = $type . "Processor";
			$this->$method($mwn, $request);
		} else {
			// error out here.
		}
	}
	
	protected function authenticate (Default_Model_MyWebBackup $mwb, $request) {
		
		$username = $request->getParam('uid');
		$password = $request->getParam('pwd');
		
		if ($mwb->authenticate($username, $password)) {
			return true;
		} else {
			return false;
		}
	}
	
	protected function backupProcessor (Default_Model_MyWebBackup $mwb, $request) {
		
	}
	
	protected function searchProcessor (Default_Model_MyWebBackup $mwb, $request) {
		
	}
	
	protected function fileProcessor (Default_Model_MyWebBackup $mwb, $request) {
		
	}
		
	protected function restoreProcessor (Default_Model_MyWebBackup $mwb, $request) {
		$fid = $request->getParam('fid');
		$file = $mwb->getStorageLocation($fid);
		if ($file !== false) {
			header("Content-Type: text/plain");
			$fp = fopen($file,"r") ;
			while (! feof($fp)) {
       			$buff = fread($fp,4096);
       			print ($buff); 
       		}
		} else {
			// error out
		}
		
		
	}

}